﻿Public Class FormPurchase

    Private Sub FormPurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'UI.FormatDataGridView(Me.dgv)
        'LoadData()

    End Sub

    Public Sub LoadData()
        'Dim cmd As String = "SELECT  p.PurchaseId, p.PurchaseNo,p.PurchaseDate,	    v.VendorName,		s.StoreName,		p.Total FROM Purchase p INNER JOIN Vendor v ON p.VendorId = v.VendorId INNER JOIN Store s ON p.StoreId = s.StoreId WHERE p.Active=1 AND  p.PurchaseDate BETWEEN '" & Me.DateTimePicker1.Value.ToString("yyyy-MM-dd") & "' AND '" & Me.DateTimePicker2.Value.ToString("yyyy-MM-dd") & "'"
        'Me.dgv.DataSource = Sql.ExecuteDataTable(cmd)


    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        'LoadData()

    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        'LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim frm As New FormPurchaseEditor()
        frm.ShowDialog()

    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

  
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        'If Me.dgv.SelectedRows.Count = 0 Then
        '    Exit Sub
        'End If

        'If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
        '    Exit Sub
        'End If

        'Sql.Begin()
        'Try
        '    Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
        '    Dim id = row.Cells("PurchaseId").Value
        '    Dim storeId = Sql.ExecuteScalar("SELECT StoreId FROM Purchase WHERE PurchaseId=" & id)
        '    Sql.ExecuteNonQuery("UPDATE Purchase SET Active=0 WHERE PurchaseId=" & id)
        '    Dim dtPurchaseDetail As DataTable = Sql.ExecuteDataTable("SELECT * FROM PurchaseDetail WHERE Active=1 AND  PurchaseId = " & id)
        '    For Each r As DataRow In dtPurchaseDetail.Rows
        '        Sql.ExecuteNonQuery("UPDATE Stock SET Qty = Qty - " & r("Qty") & " WHERE ProductId=" & r("ProductId") & " AND StoreId = " & storeId)
        '        Sql.ExecuteNonQuery("UPDATE PurchaseDetail SET Active=0 WHERE PurchaseDetailId=" & r("PurchaseDetailId"))
        '    Next

        '    Sql.Commit()
        '    LoadData()

        'Catch ex As Exception
        '    Sql.Rollback()
        '    MsgBox(ex.Message)

        'End Try 
        
    End Sub
End Class